package 反射;

//《Java Class.getMethod()/getDeclaredMethods返回Method对象》
//
//-Class.getMethod():获取当前类及所有继承的父类的public修饰的方法。仅包括public
//
//-Class.getDeclaredMethod():获取当前类的所有方法，包括public/private/protected/default修饰的方法。
//
//《定义》
//Method getMethod(String name, Class<?>... parameterTypes)  
//该方法的第一个参数name是要获得方法的名字，第二个参数parameterTypes是按声明顺序标识该方法形参类型。
//
//返回一个 Method 对象，它反映此 Class 对象所表示的类或接口的指定公共成员方法。方法后面接收的就是Class类的对象，
//而如：String.class、int.class这些字节码才是Class类的对象。
//
//如一个函数 int Test(int a, String str)对应的getMethod方法：
//
//1. getMethod("Test",int.class,String.class);
//
//2. getMethod("Test",new Class[]{int.class,String.class});
//
//例子
//实例一
/*import java.lang.reflect.*;

import 函数式编程.Test;
class TestBase {
    public String str = "hello,china!";
    protected String say() {
        return "hello,seo";
    }

   protected void say(String str,int n) {
      this.str = str;
   }
}
public class 调用方法1 {
    public static void main(String[] args) {

//    	调用方法1 cls = new 调用方法1();
//        Class<?> c = cls.getClass();
    	Class<?> c=TestBase.class;
        try {
            // parameter type is null
            //Method m = c.getDeclaredMethod("say", null);
            Method m = c.getDeclaredMethod("say");
            System.out.println("method = " + m.toString());//method = protected java.lang.String 反射.TestBase.say()

            // method String
            Class[] cArg = new Class[2];
            cArg[0] = String.class;
            cArg[1]=int.class;
            Method lMethod = c.getDeclaredMethod("say", cArg);
            System.out.println("method = " + lMethod.toString());//method = protected void 反射.TestBase.say(java.lang.String,int)
        } catch(NoSuchMethodException e) {
            System.out.println(e.toString());
        }
    }
}*/
//实例二
////Person类：
/*import java.lang.reflect.Method;
class Person1 {
    private String name;
    private int ID;
    public String speed;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public Person1(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    public void Speak() {
        System.out.println("Hello! " + "My name is " + name);
    }
    public void run(String speed) {
        System.out.println("I can run " + speed + " KM!!!");
    }
}
//testMain类：
//package fyh.reflectDemo;

public class 调用方法1 {
    public static void main(String[] args) throws Exception {
        Person1 person = new Person1("小明", 10001);
        person.Speak();//Hello! My name is 小明
        person.run("10");//I can run 10 KM!!!
        //Method m1 = person.getClass().getMethod("Speak", null);
        Method m1 = person.getClass().getMethod("Speak");
        Method m2 = person.getClass().getMethod("run", String.class);
        System.out.println(m1);//public void 反射.Person1.Speak()
        System.out.println(m2);//public void 反射.Person1.run(java.lang.String)
    }
}*/
//运行结果：
//
//Java Class.getMethod()例子
//实例三
//package com.westward;
import java.lang.reflect.Method;
class Car {
    private void bell(String time){
        System.out.println("didi---"+time);
    }
    
    public void run(){
        System.out.println("running");
    }
}
//package com.westward;

public class 调用方法1 {
    public static void main(String[] args) {
        Class <?>car = Car.class;
        Method[] methods1 = car.getMethods();
        Method[] methods2 = car.getDeclaredMethods();
        System.out.println("\n------------method.toString--------------");
        for (Method method1: methods1) {
            System.out.println("1 "+method1);//含父类的所有public方法
        }
        System.out.println("\n");
        for (Method method2: methods2) {
            System.out.println("2"+method2);
        }
        System.out.println("\n------------method.getName--------------");
        for (Method method1: methods1) {
            System.out.println(method1.getName());
        }
        System.out.println("\n");
        for (Method method2: methods2) {
            System.out.println(method2.getName());
        }
    }
}
//执行结果：
//
//------------method.toString--------------
//public void com.westward.Car.run()
//public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
//public final void java.lang.Object.wait() throws java.lang.InterruptedException
//public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
//public boolean java.lang.Object.equals(java.lang.Object)
//public java.lang.String java.lang.Object.toString()
//public native int java.lang.Object.hashCode()
//public final native java.lang.Class java.lang.Object.getClass()
//public final native void java.lang.Object.notify()
//public final native void java.lang.Object.notifyAll()
//
//
//public void com.westward.Car.run()
//private void com.westward.Car.bell(java.lang.String)

//------------method.getName--------------
//run
//wait
//wait
//wait
//equals
//toString
//hashCode
//getClass
//notify
//notifyAll